package com.zjl.paike2.controller;

import com.zjl.paike2.dto.StudentDTO;
import com.zjl.paike2.entity.StudentInfo;
import com.zjl.paike2.service.StudentInfoService;
import com.zjl.paike2.vo.PageResult;
import com.zjl.paike2.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生管理Controller
 */
@RestController
@RequestMapping("/students")
public class StudentController {
    
    @Autowired
    private StudentInfoService studentInfoService;
    
    /**
     * 查询所有学生列表（不分页）
     */
//    @GetMapping(params = "!page")
//    public Result<List<StudentInfo>> getAllStudents() {
//        List<StudentInfo> studentList = studentInfoService.getAllStudents();
//        return Result.success(studentList);
//    }
    
    /**
     * 分页条件查询学生列表
     */
    @GetMapping
    public Result<PageResult<StudentInfo>> getStudentsByCondition(
            @RequestParam(required = false) String stu_name,
            @RequestParam(required = false) String stu_id,
            @RequestParam(required = false) String class_id,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        // 输出请求参数（不包含敏感信息）
        System.out.println("接收到查询学生请求，姓名: " + stu_name + ", 学号: " + stu_id + ", 班级编号: " + class_id);
        
        PageResult<StudentInfo> pageResult = studentInfoService.getStudentsByCondition(stu_name, stu_id, class_id, page, pageSize);
        return Result.success(pageResult);
    }
    
    /**
     * 根据学号查询学生
     */
    @GetMapping("/{stuId}")
    public Result<StudentInfo> getStudentById(@PathVariable String stuId) {
        StudentInfo studentInfo = studentInfoService.getStudentById(stuId);
        if (studentInfo != null) {
            return Result.success(studentInfo);
        }
        return Result.error("学生不存在");
    }
    
    /**
     * 添加学生
     */
    @PostMapping
    public Result<Void> addStudent(@RequestBody StudentDTO studentDTO) {
        // 输出请求信息（不包含敏感信息）
        System.out.println("接收到添加学生请求，学号: " + studentDTO.getStu_id() + ", 姓名: " + studentDTO.getStu_name());
        
        // 检查请求体中的必要字段
        if (studentDTO == null) {
            return Result.error("请求体不能为空");
        }
        
        if (studentDTO.getStu_id() == null || studentDTO.getStu_name() == null) {
            return Result.error("学号和姓名不能为空");
        }
        
        // 将DTO转换为实体
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setStuId(studentDTO.getStu_id())
                  .setStuName(studentDTO.getStu_name())
                  .setClassId(studentDTO.getClass_id())
                  .setSex(studentDTO.getSex());
        
        boolean success = studentInfoService.addStudent(studentInfo);
        if (success) {
            return Result.success();
        }
        return Result.error("学号已存在");
    }
    
    /**
     * 修改学生
     */
    @PutMapping("/{stuId}")
    public Result<Void> updateStudent(@PathVariable String stuId, @RequestBody StudentDTO studentDTO) {
        // 将DTO转换为实体
        StudentInfo studentInfo = new StudentInfo();
        if (studentDTO.getStu_name() != null) {
            studentInfo.setStuName(studentDTO.getStu_name());
        }
        if (studentDTO.getClass_id() != null) {
            studentInfo.setClassId(studentDTO.getClass_id());
        }
        if (studentDTO.getSex() != null) {
            studentInfo.setSex(studentDTO.getSex());
        }
        
        boolean success = studentInfoService.updateStudent(stuId, studentInfo);
        if (success) {
            return Result.success();
        }
        return Result.error("学生不存在");
    }
    
    /**
     * 删除学生
     */
    @DeleteMapping("/{stuId}")
    public Result<Void> deleteStudent(@PathVariable String stuId) {
        boolean success = studentInfoService.deleteStudent(stuId);
        if (success) {
            return Result.success();
        }
        return Result.error("学生不存在");
    }
} 